package com.xlh.outside;
/**
 * 管理个人添加的服务器页面
 */

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wzm.param.ApiResultPo.LoginReslult;
import com.wzm.param.ApiResultPo.UserServerReslult;
import com.xlh.Utils.GsonManager;
import com.xlh.conf.CommonConf;
import com.xlh.interf.IUiUpdateHandler;
import com.xlh.lib.Core.FIntent;
import com.xlh.thread.TestTask;

/**
 * Created by mike on 2017-12-19.
 */

public class ManagerServerActivity extends  BaseActivity implements IUiUpdateHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_manager_server_main);
        app = (WzApplication)getApplication();
        bindControl();
        ininControlEvent();


    }


    private void bindControl(){
        cTxtback = (TextView)findViewById(R.id.mgserver_txt_title_tag_left);
        cEdtSvrName = (EditText) findViewById(R.id.mgserver_edt_svr_name);
        cEdtSvrHost = (EditText) findViewById(R.id.mgserver_edt_svr_host);
        cEdtWzkey = (EditText) findViewById(R.id.mgserver_edt_wzkey);
        cButSubmit  = (Button) findViewById(R.id.mgserver_but_submit);
    }



    @Override
    public Handler getHandler() {
        return handler;
    }

    /**
     * 初始化事件
     */
    private void ininControlEvent(){





        handler = new Handler(){

            @Override
            public void handleMessage(Message msg) {

                String res  = msg.getData().getCharSequence("result").toString();
                UserServerReslult data = GsonManager.getIns().jsonToAnyObject(res,UserServerReslult.class);
                progressDlg.dismiss();
                if(msg.getData().get("type").equals("getver")){


                    if(data.getCode()==1){
                        cEdtSvrName.setText(data.getData().getSname());
                        cEdtSvrHost.setText(data.getData().getUrl());
                        cEdtWzkey.setText(data.getData().getWzkey());
                    }
                }else{
                    Toast.makeText(ManagerServerActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
                }

                super.handleMessage(msg);
            }
        };



        cButSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String svrName,svrHost,wzKey,param;

                svrName = cEdtSvrName.getText().toString();
                svrHost = cEdtSvrHost.getText().toString();
                wzKey = cEdtWzkey.getText().toString();
                param = String.format("username=%s&svrname=%s&svrhost=%s&wzkey=%s",app.getActiveUser(),svrName,svrHost,wzKey);



                Thread th = new Thread(new TestTask()
                        .setContext(ManagerServerActivity.this)
                        .setUrl(CommonConf.API_EDIT_USER_SVR + param)
                        .setType("editUserServer")
                );

                th.start();




            }
        });


        //back
        cTxtback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FIntent fIntent = new FIntent(ManagerServerActivity.this, SettingActivity.class);
                startActivity(fIntent);
                finish();
            }
        });



        progressDlg = new ProgressDialog( ManagerServerActivity.this);

        progressDlg.setMessage("请稍后,正在获取单机服务器数据....");
        progressDlg.setIcon(R.drawable.ic_launcher);
        progressDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDlg.setCancelable(false);
        progressDlg.show();

        Thread th = new Thread(new TestTask()
                .setContext(ManagerServerActivity.this)
                .setUrl(CommonConf.API_GET_USER_SVR+"username="+app.getActiveUser())
                .setType("getver")
        );
        th.start();



    }






    /**
     * 用户名控件
     */
    private EditText cEdtSvrName;
    private EditText cEdtSvrHost;
    private EditText cEdtWzkey;

    private Button cButSubmit;

    private TextView cTxtback;

    private Handler handler;
    private WzApplication app;
    private ProgressDialog progressDlg;




}
